// class Player {
//     constructor(name) {
//         this.name = name;
//         this.enemy = null;
//     }
//     win() {
//         console.log(this.name,"win---");
//     }
//     lose() {
//         console.log(this.name,"lose---");
//     }
//     die() {
//         this.lose()
//         this.enemy.win()
//     }
// }
// const player1 = new Player("张俊杰1")
// const player2 = new Player("张俊杰2")

// player1.enemy = player2;
// player2.enemy = player1;

// player1.die()

// 为游戏增加玩家

class Player {
    constructor(name,teamColor) {
        this.name = name;
        this.enemy = [];
        this.partner = [];
        this.stats = 'live';
        this.teamColor = teamColor;
    }
    win() {
        console.log(this.name,"win---");
    }
    lose() {
        console.log(this.name,"lose---");
    }
    die() {
        var all_die = true;
        this.stats = 'die'

        for(let i = 0, partner; partner = this.partner[i++];){
            if(partner.stats !== 'die'){
                all_die = false;
                break;
            }
        }
        if(all_die) {
            this.lose();
            for(let i = 0, partner; partner = this.partner[i++];){
                partner.lose()
            }
            for(let i = 0, enemy; enemy = this.enemy[i++];){
                enemy.win()
            }
        }
    }
}

var players = [];
var playerFactory = function (name, teamColor) {
    var newPlayer = new Player(name,teamColor)
    for(let i =0, player; player = players[i++];){
        if(player.teamColor === newPlayer.teamColor){
            player.partner.push(newPlayer);
            newPlayer.partner.push(player);
        }else {
            player.enemy.push(newPlayer);
            newPlayer.enemy.push(player);
        }
    }
    players.push(newPlayer)
    return newPlayer
}
//红队
var player1 = playerFactory("张俊杰1",'red')
var player2 = playerFactory("张俊杰2",'red')
var player3 = playerFactory("张俊杰3",'red')
var player4 = playerFactory("张俊杰4",'red')
// 蓝队
var player5 = playerFactory("张俊杰5",'blue')
var player6 = playerFactory("张俊杰6",'blue')
var player7 = playerFactory("张俊杰7",'blue')
var player8 = playerFactory("张俊杰8",'blue')

player1.die()
player2.die()
player3.die()
player4.die()